package com.rz.htk.template.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.rz.core.exception.GlobalException;
import com.rz.htk.template.entity.Template;
import com.rz.htk.template.entity.TemplateField;
import com.rz.htk.template.entity.vo.TemplateDeleteParam;
import com.rz.htk.template.mapper.TemplateFieldMapper;
import com.rz.htk.template.service.ITemplateFieldService;
import com.rz.htk.template.service.ITemplateService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName TemplateFieldServiceImpl
 * @Description 模板字段信息
 * @Author maweiyue
 * @Date 2019/3/26 20:40
 */
@Service
public class TemplateFieldServiceImpl extends ServiceImpl<TemplateFieldMapper, TemplateField> implements ITemplateFieldService {

    @Resource
    private ITemplateService templateService;

    @Override
    public IPage<TemplateField> getPage(TemplateField Field) {
        QueryWrapper<TemplateField> wrapper = new QueryWrapper();
        wrapper.eq("temp_id", Field.getTempId());
        Page<TemplateField> page = new Page<>(Field.getPageNo(), Field.getPageSize());
        IPage<TemplateField> FieldIPage = page(page, wrapper);
        this.buildResult(FieldIPage.getRecords());
        return FieldIPage;
    }

    private void buildResult(List<TemplateField> list){
        if(list.isEmpty()){
            return;
        }
        List<Integer> tempIds = new ArrayList<>();
        list.forEach(Field -> {
            tempIds.add(Field.getTempId());
        });
        List<Template> prdList = (List<Template>) templateService.listByIds(tempIds);
        list.forEach(templateField -> {
            prdList.forEach(template -> {
               if (templateField.getTempId().equals(template.getId())) {
                   templateField.setTempName(template.getName());
               }
            });
        });
    }

    @Transactional
    public boolean saveTemplateField(TemplateField field){
        if (field.getId() == null) {
            TemplateField temp = getTemplateFieldByName(field.getTempId(), field.getName());
            if (temp != null) {
                throw new GlobalException("该模板已存在字段【" + field.getName() + "】！");
            }
        } else {
            TemplateField temp = getTemplateFieldByName(field.getTempId(), field.getName());
            if (temp != null && !temp.getId().equals(field.getId())) {
                throw new GlobalException("该模板已存在字段【" + field.getName() + "】！");
            }
        }
        boolean isOk = this.saveOrUpdate(field);
        if (!isOk) {
            throw new GlobalException("字段保存失败！");
        }
        return true;
    }

    /**
     * 根据字段名称获取字段信息
     * @param tempId  模板id
     * @param fieldName  字段名称
     * @return
     */
    public TemplateField getTemplateFieldByName(Integer tempId, String fieldName) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("temp_id", tempId);
        queryWrapper.eq("name", fieldName);
        List<TemplateField> templateFieldList = list(queryWrapper);
        if (CollectionUtils.isNotEmpty(templateFieldList)) {
            return templateFieldList.get(0);
        }
        return null;
    }

    @Override
    @Transactional
    public boolean batchDelete(TemplateDeleteParam prdDeleteParam){
        prdDeleteParam.getIds().forEach(id -> {
            TemplateField field = this.getById(id);
            if (field != null) {

                boolean isOk = this.removeById(field.getId());
                if (!isOk) {
                    throw new GlobalException("字段【" + field.getName() + "】删除失败！");
                }
            } else {
                throw new GlobalException("字段【" + field.getName() + "】不存在，删除失败！");
            }
        });
        return true;
    }

    @Override
    public List<TemplateField> listByTemplateId(Integer tempId){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("temp_id", tempId);
        return list(queryWrapper);
    }
}
